#ifndef TALKSERVER_H
#define TALKSERVER_H
#include <QObject>
#include <QTcpServer>
#include <QDebug>
#include <QTcpSocket>

class talkServer : public QObject
{
    Q_OBJECT
public:
    explicit talkServer(QObject *parent = nullptr);

    void run()
    {
        m_TcpServer.listen(QHostAddress::AnyIPv4, 1234);
        connect(&m_TcpServer, &QTcpServer::newConnection, this, &talkServer::dealNewCon);
        qDebug()<<"server run";
    }

    void dealNewCon()
    {
        auto *socket = m_TcpServer.nextPendingConnection();
        qDebug()<<"new connect"<<socket->peerAddress().toString();
        tcpInfo info{socket};

        m_lTcpInfo<<info;
        connect(socket, &QTcpSocket::readyRead, this, &talkServer::dealRead);
        connect(socket, &QTcpSocket::disconnected, this, &talkServer::dealdiscon);

    }

    void dealdiscon()
    {
        QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
        qDebug()<<"close connect"<<socket->peerAddress().toString();
        for(int i = 0; i <m_lTcpInfo.size(); i++)
        {
            if(m_lTcpInfo[i].socket == socket)
            {
                m_lTcpInfo.erase(m_lTcpInfo.begin()+1);
                break;
            }
        }
    }
    void dealRead()
    {
        QTcpSocket *socket = static_cast<QTcpSocket *>(sender());

        auto data = socket->readAll();

        for(int i = 0; i <m_lTcpInfo.size(); i++)
        {
            if(m_lTcpInfo[i].socket == socket)
            {
                m_lTcpInfo[i].arrData.append(data);
                dealPack(i);
                break;
            }
        }
        //qDebug()<<m_TcpSocket->readAll();
    }

    void dealPack(int i)
    {
        auto info = &m_lTcpInfo[i];
        if(info->arrData.size()>=4 && info->arrData[0]==0x66)
        {
            quint16 len = (info->arrData[2]<<8 & 0xFF00)| static_cast<quint16>(info->arrData[1]);

            if(info->arrData.size() >= len)
            {
                QByteArray arr = info->arrData.mid(0, len);
                dealRec(i, arr);
                info->arrData = info->arrData.mid(len);
            }
        }
    }

    void dealRec(int i, QByteArray arr)
    {
        auto info = m_lTcpInfo[i];

        char cmd = arr[3];
        QByteArray arrData = arr.mid(4);

        do
        {
            if(cmd == 0x01)
            {
                //心跳反馈

                break;
            }
            if(cmd == 0x03)
            {
                auto strIP = info.socket->localAddress().toString() + ":";
                qDebug()<<strIP<<":"<<QString(arrData);
                arrData.insert(0, strIP.toUtf8());

                QByteArray arrSendBuffer;
                arrSendBuffer.append(0x66);

                quint16 l = arrData.size()+4;

                arrSendBuffer.append(static_cast<char>(l&0x00FF));
                arrSendBuffer.append(static_cast<char>(l>>8&0x00FF));
                arrSendBuffer.append(0x03);
                arrSendBuffer.append(arrData);

                for(int i = 0; i<m_lTcpInfo.size(); i++)
                {
                    if(m_lTcpInfo[i].socket == info.socket)
                    {
                        arrSendBuffer[3] = 0x02;
                        m_lTcpInfo[i].socket->write(arrSendBuffer);
                        continue;
                    }

                    arrSendBuffer[3] = 0x03;
                    m_lTcpInfo[i].socket->write(arrSendBuffer);
                }
                break;
            }
        }while(false);

    }
signals:

private:
    QTcpServer m_TcpServer;

    struct tcpInfo
    {
        QTcpSocket* socket;
        QByteArray arrData;
    };

    QList<tcpInfo> m_lTcpInfo;
};

#endif // TALKSERVER_H
